package com.caoustc.okhttplib.okhttp

import android.text.TextUtils
import com.caoustc.okhttplib.okhttp.https.HttpsCerManager
import okhttp3.Headers
import okhttp3.OkHttpClient
import java.io.InputStream
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier

class OkHttpManager private constructor() {
    @get:Deprecated("")
    var okHttpClient: OkHttpClient? = null
        private set
    var configration: OkHttpConfiguration? = null
        private set
    var isReturnSocketException = false
    var httpErrorManager: OkHttpErrorManager? = null
    var urlHead: String? = null
    var platformUrl: String? = null
    @Synchronized
    fun init(configuration: OkHttpConfiguration) {
        configration = configuration
        val timeout = configuration.timeout
        val builder = OkHttpClient.Builder()
            .connectTimeout(timeout, TimeUnit.MILLISECONDS)
            .writeTimeout(timeout, TimeUnit.MILLISECONDS)
            .readTimeout(timeout, TimeUnit.MILLISECONDS)
        if (configuration.hostnameVerifier != null) {
            builder.hostnameVerifier(configuration.hostnameVerifier)
        }
        val certificateList = configuration.certificateList
        if (certificateList != null && certificateList.size > 0) {
            val httpsCerManager = HttpsCerManager(builder)
            httpsCerManager.setCertificates(certificateList)
        }
        val cookieJar = configuration.cookieJar
        if (cookieJar != null) {
            builder.cookieJar(cookieJar)
        }
        if (configuration.cache != null) {
            builder.cache(configuration.cache)
        }
        if (configuration.authenticator != null) {
            builder.authenticator(configuration.authenticator)
        }
        if (configuration.certificatePinner != null) {
            builder.certificatePinner(configuration.certificatePinner)
        }
        builder.followRedirects(configuration.isFollowRedirects)
        builder.followSslRedirects(configuration.isFollowSslRedirects)
        if (configuration.sslSocketFactory != null) {
            builder.sslSocketFactory(configuration.sslSocketFactory)
        }
        if (configuration.dispatcher != null) {
            builder.dispatcher(configuration.dispatcher)
        }
        builder.retryOnConnectionFailure(configuration.isRetryOnConnectionFailure)
        builder.networkInterceptors().addAll(configuration.networkInterceptorList)
        if (configuration.interceptorList != null) {
            builder.interceptors().addAll(configuration.interceptorList)
        }
        if (configuration.proxy != null) {
            builder.proxy(configuration.proxy)
        }
        OkHttpConstants.DEBUG = configuration.isDebug
        okHttpClient = builder.build()
    }

    fun updateCommonParams(key: String?, value: String?) {
        var add = false
        val commonParams = configration?.commonParams
        if (commonParams != null) {
            for (param in commonParams) {
                if (TextUtils.equals(param.key, key)) {
                    param.value = value
                    add = true
                    break
                }
            }
            if (!add) {
                commonParams.add(Part(key, value))
            }
        }
    }

    fun updateCommonHeader(key: String?, value: String?) {
        var headers = configration?.commonHeaders
        if (headers == null) {
            headers = Headers.Builder().build()
        }
        configration!!.commonHeaders = headers!!.newBuilder().set(key, value).build()
    }

    fun removeCommonHeader(key: String?) {
        val headers = configration?.commonHeaders
        if (headers != null) {
            configration?.commonHeaders = headers.newBuilder().removeAll(key).build()
        }
    }

    fun clearCommonHeader() {
        configration!!.commonHeaders = null
    }

    val okHttpClientBuilder: OkHttpClient.Builder
        get() = okHttpClient!!.newBuilder()
    val commonParams: List<Part?>?
        get() = if (null == configration) {
            null
        } else configration?.commonParams
    val certificateList: List<InputStream?>?
        get() = configration?.certificateList
    val hostnameVerifier: HostnameVerifier?
        get() = configration?.hostnameVerifier
    val timeout: Long?
        get() = configration?.timeout
    val commonHeaders: Headers?
        get() = if (null == configration) {
            null
        } else configration?.commonHeaders

    companion object {
        private var okHttpManager: OkHttpManager? = null
        @kotlin.jvm.JvmStatic
        val instance: OkHttpManager?
            get() {
                if (okHttpManager == null) {
                    okHttpManager = OkHttpManager()
                }
                return okHttpManager
            }
    }
}